package ru.sgu.diploma.practic.utils;

import ru.sgu.diploma.practic.domain.model.GeometricModel;
import ru.sgu.diploma.practic.domain.model.ImageResolution;
import ru.sgu.diploma.practic.domain.model.Point;


import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.regex.Matcher;


/**
 * User: datmakin
 * Date: 13.03.11
 * Email: atmakin.dv@gmail.com
 */
public class GeometricModelUtils {
    public static ImageResolution calculateResolution(GeometricModel model) {
        List<Point> pointList = model.getPointList();

        //calculate an image total resolution

        Integer maxX = 0;
        Integer maxY = 0;

        Integer minX = 0;
        Integer minY = 0;

        for (Point point : pointList) {
            if (point.getX() > maxX)
                maxX = point.getX();
            if (point.getY() > maxY)
                maxY = point.getY();
            if (point.getX() < minX)
                minX = point.getX();
            if (point.getY() < minY)
                minY = point.getY();
        }

        Integer width = Math.abs(minX.intValue()) + maxX.intValue() + 20;
        Integer height = Math.abs(minY.intValue()) + maxY.intValue() + 20;

        ImageResolution imageResolution = new ImageResolution(height, width);

        //calculate a center of the image

        Integer x = Math.abs(minX) + 10;
        Integer y = Math.abs(minY) + 10;

        imageResolution.setBegin(new Point(x.intValue(), y.intValue()));

        return imageResolution;
    }

    public static BufferedImage verticalflip(BufferedImage img) {
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage dimg = new BufferedImage(w, h, img.getColorModel()
                .getTransparency());
        Graphics2D g = dimg.createGraphics();
        g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
        g.dispose();
        return dimg;
    }
}
